const Service = require('egg').Service;

class MemberFsllService extends Service {

  async create(member) {
    const lastMemberId = await this.app.mysql.query(`SELECT MAX(id) FROM member;`);
    const memberCreate = {
      ...member,
      id: lastMemberId[0]['MAX(id)'] + 1,
      sortId: lastMemberId[0]['MAX(id)'] + 1,
      joinState: 1,
      position: '新人'
    };
    const result = await this.app.mysql.insert('member', memberCreate);

  if (result.affectedRows === 1) {
      const updateMemberTreeResult = await this.app.mysql.query('CALL updateMemberTree();');
      const updateCountResult = await this.app.mysql.query('CALL updateOrgCount();');
   }

    return result;
  }

  async update(member) {
    const result = await this.app.mysql.update('member', member);

    if (result.affectedRows === 1) {
      const updateMemberTreeResult = await this.app.mysql.query('CALL updateMemberTree();');
      const updateCountResult = await this.app.mysql.query('CALL updateOrgCount();');
    }
    return result;
  }

}

module.exports = MemberFsllService;